<template>
  <!--  工单进度 -->
  <div class="content">
    <div class="pageSearchView">
      <el-form :inline="true" :model="searchParam" :rules="formRules" ref="searchForm" class="demo-form-inline">
        <el-row :gutter="0">
          <el-form-item label="工单号">
            <el-input v-model="searchParam.workNo" placeholder="请输入工单号" clearable style="width: 160px" />
          </el-form-item>
          <el-form-item label="工单状态">
            <el-select v-model="searchParam.status" placeholder="请选择工单状态" clearable style="width: 160px">
              <el-option v-for="item in dicData" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="设备">
            <el-select v-model="searchParam.equipmentId" placeholder="请选择设备" clearable filterable remote reserve-keyword
              :remote-method="remoteEquipment" style="width: 160px">
              <el-option v-for="item in equipmentOptions" :key="item.id" :label="item.equipmentName" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="产出编码">
            <el-input v-model="searchParam.outMaterialNo" placeholder="请输入产出编码" clearable style="width: 160px" />
          </el-form-item>
          <el-form-item label="计划时间">
            <el-date-picker v-model="searchParam.times" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss"
              range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 360px;" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="submitForm()">查询</el-button>
            <el-button class="refreshBtn" :icon="Refresh" @click="resetSearch()">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="pageTableView">
      <div class="pageTableMenuView">
        <div class="pageTableTitle">列表信息</div>
      </div>
      <!-- 表格 -->
      <el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 320px)">
        <el-table-column label="序号" align="center" width="70" fixed>
          <template #default="scope"> {{ (pageSearch.current - 1) * pageSearch.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="workNo" label="工单号" align="center" min-width="200" fixed />
        <el-table-column prop="status" label="工单状态" align="center" min-width="200">
          <template #default="{ row }"> {{ dicData.find(i => i.value === row.status)?.label }} </template>
        </el-table-column>
        <el-table-column prop="organizationName" label="功能位置" align="center" min-width="200" />
        <el-table-column prop="equipmentCode" label="设备编号" align="center" min-width="200" show-overflow-tooltip />
        <el-table-column prop="equipmentName" label="设备名称" align="center" min-width="200" show-overflow-tooltip />
        <el-table-column prop="productCode" label="产品编码" align="center" min-width="200" show-overflow-tooltip />
        <el-table-column prop="outMaterialNo" label="产出编码" align="center" min-width="200" show-overflow-tooltip />
        <el-table-column prop="planCount" label="计划生产数量" align="center" min-width="200" />
        <el-table-column prop="alreadyCount" label="报工数量" align="center" min-width="200" />
        <el-table-column prop="scrapCount" label="报废数量" align="center" min-width="200" />
        <el-table-column prop="notAlreadyCount" label="工单未生产数量" align="center" min-width="200" />
        <el-table-column prop="workRate" label="工单生产进度" align="center" min-width="200" />
        <el-table-column prop="firstDate" label="首次报工时间" align="center" min-width="200" />
        <el-table-column prop="putMaterialCount" label="投料数量" align="center" min-width="200" />
        <el-table-column prop="planBeginTime" label="计划开始时间" align="center" min-width="200" />
        <el-table-column prop="planEndTime" label="计划结束时间" align="center" min-width="200" />
        <el-table-column prop="actualBeginTime" label="实际开始时间" align="center" min-width="200" />
        <el-table-column prop="actualEndTime" label="实际结束时间" align="center" min-width="200" />
      </el-table>
      <div class="pagePagination">
        <el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
          :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next" :total="pageTotal"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { ElMessage, ElForm } from 'element-plus';
import axiosInstance from '@/utils/http/api';
import { Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue';
import dayjs from 'dayjs';

onMounted(() => {
  getList();
  getOption();
});

const searchForm = ref<InstanceType<typeof ElForm>>();

const pageSearch = ref({
  current: 1,
  pageSize: 10
});

const dicData = ref([
  { label: "新建", value: 0 },
  { label: "待排产", value: 1 },
  { label: "已排产", value: 2 },
  { label: "下发", value: 3 },
  { label: "开工", value: 4 },
  { label: "中断", value: 5 },
  { label: "暂停", value: 6 },
  { label: "完成", value: 7 },
]);


const pageTotal = ref(null); // 页面总数
const searchParam = ref({
  // times: [
  //   dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss'),
  //   dayjs().format('YYYY-MM-DD HH:mm:ss')
  // ],
});

const paginatedData = ref([]);
const workRoomList = ref([]); // 车间
const equipmentData = ref([]);
const equipmentOptions = ref([])

const formRules = ref({
  organizationId: [{ required: true, message: '请选择工厂/车间', trigger: 'change' }],
  equipmentIds: [{ required: true, message: '请选择设备', trigger: 'change' }]
});

const remoteEquipment = (value) => {
  if (!value) {
    return
  }
  axiosInstance.post('/api/basedataEquipment/getPage', {
    current: 1,
    pageSize: 50,
    equipmentName: value
  })
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      equipmentOptions.value = res.data.records;
    });
}

/**
 * 获取车间列表
 */
const getOption = () => {

  axiosInstance.post('/api/basedataEquipment/getPage', {
    current: 1,
    pageSize: 50
  })
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      equipmentOptions.value = res.data.records;
    });
  axiosInstance.post('/api/basedataOrganization/getAll', {})
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      workRoomList.value = res.data;
    });
};

/**
 * 获取列表数据
 */
const getList = () => {
  axiosInstance.post('/api/workOrder/reportworkOrderReport', {
    ...searchParam.value,
    startDate: searchParam.value?.times?.[0],
    endDate: searchParam.value?.times?.[1],
    times: undefined,
    ...pageSearch.value
  })
    .then(res => {
      if (res.code !== 200) {
        ElMessage.error(res.message);
        return false;
      }
      paginatedData.value = res.data.records;
      pageTotal.value = res.data.total;
    });
};

/**
 * 分页大小变化时处理
 * @param e 新的分页大小
 */
const handleSizeChange = (e) => {
  pageSearch.value.pageSize = e;
  getList();
};

/**
 * 当前页变化时处理
 * @param e 新的当前页
 */
const handleCurrentChange = (e) => {
  pageSearch.value.current = e;
  getList();
};

/**
 * 提交表单
 */
const submitForm = () => {
  searchForm.value.validate((valid) => {
    if (valid) {
      getList();
    } else {
      ElMessage.error('请检查表单输入');
      return false;
    }
  });
};

/**
 * 重置搜索条件
 */
const resetSearch = () => {
  pageSearch.value.current = 1;
  searchForm.value.clearValidate();
  searchParam.value = {
    times: [
      dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss'),
      dayjs().format('YYYY-MM-DD HH:mm:ss')
    ]
  };
  submitForm()
};
</script>
<style scoped></style>